home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d1 / basictut.arc / A4.TXT < prev    next >
Text File  |  1989-01-07  |  12KB  |  251 lines

  1. -----
  2. REM Statement
  3.  
  4.         All right!  You have successfully completed three lessons in BASIC.  By
  5. now you should be fairly confident with the language.
  6.  
  7.         The |REM~ statement allows you to insert explanatory |rem~arks into the
  8. program.  These remarks are |ignored~ by the computer when it executes the
  9. program, but provide the programmer with information about the program.  They
  10. appear only when you LIST the program.  The REM statement can begin with either
  11. the letters |REM~ or an apostrophe (|'~) followed by the remark itself.
  12.  
  13.         Here is an example of REM statements in a program.
  14.  
  15.                 10 DATA 45,24,97,65,47,15,80,38,77,999
  16.                 20 READ X:IF X=999 THEN 70      |'If end of data goto line 60.~
  17.                 30 IF X>=50 THEN LET G=G+1      |'Count numbers G.T.E. to 50.~
  18.                 40 IF X<50 THEN LET L=L+1       |'Count number L.T. 50.~
  19.                 50 GOTO 20                      |'Get the next number.~
  20.                 60 PRINT "Of the numbers,";L;"were less than 50 and";
  21.                 70 PRINT G;"were greater than or equal to 50."
  22.  
  23.         Try typing in and |RUN~ning this program.
  24. -----
  25. STOP and END Statements
  26.  
  27.         There are four ways to stop the execution of a program in BASIC.
  28.  
  29.         1.  The program automatically stops when it runs out of lines to
  30.             execute.
  31.         2.  |STOP~ Statement.  When the computer comes to a STOP statement, it
  32.             prints |Break in N~ (where |N~ is the |line number~ of the STOP
  33.             statement) and stops.
  34.         3.  |END~ Statement.  This is similar to the STOP statement except that
  35.             the computer does not print the Break message.
  36.         4.  Pressing the |Ctrl~ key and the |Break~ key (Scroll lock) together
  37.             stops the program during execution and prints a |Break in N~
  38.             message.  If you want to continue with the program, simply type
  39.             |CONT~ (short for |cont~inue) and press the return key.  If you haven't
  40.             edited the program, the computer will continue with the program
  41.             right where it was interrupted (this works if you used a STOP
  42.             statement as well, but not if you used an END statement).
  43. -----
  44. STOP and END Statements (continued)
  45.  
  46.         Try out these statements and commands by typing in and |RUN~ning the
  47. following program.
  48.  
  49.                 |10 PRINT "Type CONT and press return."~
  50.                 |20 STOP~
  51.                 |30 PRINT "GOOD!  You continued the program."~
  52.                 |40 PRINT "Use the Ctrl and Break keys any time now!":GOTO 40~
  53. -----
  54. EDIT Command
  55.  
  56.         When you make (or discover) a mistake in a program line, you can
  57. correct it by simply typing the line over.  However, it is much faster to use
  58. the BASIC Program Editor.  To do this, simply type |EDIT N~ (where |N~ is the line
  59. number to edit) and then move to the mistake using the cursor control keys, and
  60. correct it by typing over, inserting or deleting.
  61.  
  62.         If you want to completely erase that line of the program, you can do so
  63. by typing the line number itself and pressing return.  If there are several
  64. lines you want to delete, type |DELETE N1-N2~ (where |N1~ is the first line and |N2~
  65. is the last line to delete) and press return.  This will delete all lines
  66. between and including |N1~ and |N2~.
  67. -----
  68. AUTO Command
  69.  
  70.         When you are first writing a program, you spend a good deal of time
  71. just typing in the line numbers.  The |AUTO~ command will take care of that for
  72. you.  Type the word AUTO and the computer will put the line numbers in
  73. automatically.
  74.  
  75.         Here is the correct form, or syntax, for the AUTO command.
  76.  
  77.                                 AUTO [|B~[,|I~]]
  78.  
  79.         |B~ is the line number at which the AUTO command will begin.  If you do
  80. not give a value for |B~ the computer will |default~ to (automatically start
  81. at) line number 10.
  82.  
  83.         |I~ is the increment value.  It is the value that will be added to each
  84. line number to get the next line number.  If you do not enter a number for |I~,
  85. it will default to 10.
  86. -----
  87. AUTO Command (continued)
  88.  
  89.         If you want to start at some line number other than 10, say 200, type
  90. AUTO 200 and your line numbers will begin at 200 and increase in increments of
  91. 10.  If you want to start at 100 and increase by 5's then type AUTO 100,5.
  92. When you finish typing in your program, shut off the AUTO command by pressing
  93. the |Ctrl~ key and the |Break~ key together.
  94.  
  95.         Try typing in the following program using the AUTO command.
  96.  
  97.                 |100 INPUT "What is the first number";A~
  98.                 |105 INPUT "What is the second number";B~
  99.                 |110 PRINT A;"+";"B";"=";A+B~
  100.                 |115 PRINT A;"-";"B";"=";A-B~
  101.                 |120 PRINT A;"*";"B";"=";A*B~
  102. -----
  103. TRON and TROFF Commands
  104.  
  105.         If a program doesn't work properly when you try running it, it is said
  106. to have a |bug~ in it.  Much of a programmer's time is spent |de-bugging~ his
  107. or her programs.  A feature that sometimes makes this easier is the |TRON~
  108. command.  TRON is short for program |TR~acer |ON~.  When you type TRON and then RUN
  109. your program, the computer will print each line number, in brackets (|[]~) as
  110. it is executed.  Other program output will also be printed, but only the
  111. executed line numbers will be printed in brackets.
  112.  
  113.         For this program:       |10 DATA 17,23,99~
  114.                                 |20 READ NUM~
  115.                                 |30 IF NUM<>99 THEN PRINT NUM:GOTO 20~
  116.                                 |40 PRINT "The END."~
  117.         The output using TRON
  118.         would look like this:   |[10][20][30] 17~
  119.                                 |[20][30] 23~
  120.                                 |[20][30][40]The END.~
  121.  
  122.         When you have found your bug and want to run your program without the
  123. line numbers being displayed, type |TROFF~ which is short for program |TR~acer |OFF~.
  124. This returns the computer to normal program execution.
  125. -----
  126. TRON and TROFF Commands (continued)
  127.  
  128.         The following program is supposed to count and print the values in a
  129. DATA statement, but for some reason it gets stuck on the first value and goes
  130. crazy.  By using the TRON and TROFF commands, try and find the bug in this
  131. program.
  132.  
  133.                 |10 DATA 34,67,12,9,65,38,84,999~
  134.                 |20 READ NUM:IF NUM=999 THEN GOTO 60~
  135.                 |30 COUNT=COUNT+1~
  136.                 |40 PRINT "Value number";COUNT;"is";NUM~
  137.                 |50 GOTO 30~
  138.                 |60 PRINT "There are";COUNT;"numbers in the data statement."~
  139.  
  140.         First, type in the program and try to |RUN~ it.  After a few seconds
  141. press the |Ctrl~ and |Break~ keys to stop the program.  Then type |TRON~ to turn the
  142. tracer on.  Again |RUN~ the program and when you have identified the problem,
  143. stop the program and fix the bug.  Now turn the tracer off by typing |TRON~ and
  144. |RUN~ the program a third time.
  145. -----
  146. TRON and TROFF Commands (continued)
  147.  
  148.         Here is the same program from the last page.  Hopefully you found the
  149. bug in line |50~.
  150.  
  151.                 10 DATA 34,67,12,9,65,38,84,999
  152.                 20 READ NUM:IF NUM=999 THEN GOTO 60
  153.                 30 COUNT=COUNT+1
  154.                 40 PRINT "Value number";COUNT;"is";NUM
  155.                 |50 GOTO 30~
  156.                 60 PRINT "There are";COUNT;"numbers in the data statement."
  157.  
  158.         The line should have read |50 GOTO 20~.  As it was, the program never
  159. read a new value for the variable |NUM~, so it never got to |999~ to signal the
  160. |end of DATA~.
  161.  
  162.         If you found the bug, congratulations!  If not make sure you understand
  163. how the TRON and TROFF commands work and better luck next time.
  164. -----
  165. RND Function
  166.  
  167.         The |RND~ function returns a random number between 0 and 1.  If you
  168. want a random number between 0 and 1000, then just multiply by 1000.
  169.  
  170.                 example- LET |X=RND~       [this returns |X~ such that |0~ < |X~ <| 1~]
  171.                          LET |Y=RND*100~   [this returns |Y~ such that |0 ~< |Y~ <| 100~]
  172.  
  173.         Here is a sample program that lets you input an |upper bound~ number,
  174. and the computer prints a list of random numbers between 0 and your upper
  175. bound.  You will have to use the |Ctrl~ and |Break~ keys to stop this program.
  176.  
  177.                 |10 INPUT "Enter a number to be the upper bound.",MAX~
  178.                 |20 PRINT RND*MAX~
  179.                 |30 GOTO 20~
  180. -----
  181. INT Function
  182.  
  183.         The |INT~ function returns the largest integer less than or equal to
  184. the number in parentheses.
  185.  
  186.                 example- |LET A=INT(9.2)~    [in this statement, |A~ will equal  |9~]
  187.                          |LET K=INT(-7.6)~   [in this statement, |K~ will equal |-8~]
  188.  
  189.         Here is a program that uses the RND and INT functions to quiz you on
  190. the INT function.  Once again, press the |Ctrl~ and |Break~ keys when you have
  191. had enough.
  192.  
  193.         |10 NUMBER=RND*200-100~ 'pick a random number between -100 and 100
  194.         |20 PRINT "What is the INT of";NUMBER;:INPUT ANSWER~
  195.         |30 IF ANSWER<>INT(NUMBER) THEN PRINT "The answer is";INT(NUMBER)~
  196.         |40 GOTO 10~
  197. -----
  198. ABS Function
  199.  
  200.         The |ABS~ function returns the |absolute value~ of an expression.  The
  201. absolute value is just the positive value of a number.
  202.  
  203.                 example- |LET X=ABS(-5)~    [in this statement, |X~ will equal  |5~]
  204.                          |LET C=ABS(12*4)~  [in this statement, |C~ will equal |48~]
  205.  
  206.         Here is a program that uses the RND, INT and ABS functions to quiz you
  207. on the ABS function.  Once again, press the |Ctrl~ and |Break~ keys when you
  208. have had enough.
  209.  
  210.         |10 NUMBER=INT(RND*200-100)~ 'pick integer between -100 and 100
  211.         |20 PRINT "What is the ABS of";NUMBER;:INPUT ANSWER~
  212.         |30 IF ANSWER<>ABS(NUMBER) THEN PRINT "The answer is";ABS(NUMBER)~
  213.         |40 GOTO 10~
  214. -----
  215. ABS and INT functions (continued)
  216.  
  217.         Here is another short program that you can try out.  It uses the ABS
  218. and INT functions to find the |G~reatest |C~ommon |D~enominator of two numbers.
  219.  
  220.                 |10 INPUT "Enter the first number.",NUM1~
  221.                 |20 INPUT "Enter the second number.",NUM2~
  222.                 |30 NUM1=ABS(NUM1):NUM2=ABS(NUM2)~
  223.                 |40 LET R=NUM1-NUM2*INT(NUM1/NUM2)~
  224.                 |50 IF R=0 THEN GOTO 70~
  225.                 |60 NUM1=NUM2:NUM2=R:GOTO 40~
  226.                 |70 PRINT "The greatest common denominator is";NUM2~
  227. -----
  228. End of Lesson Four
  229.  
  230.         Congratulations on completing the |Beginning BASIC~ Course.  Now to bring
  231. together much of what you've learned, try writing the following programs.
  232.  
  233.         1. A program that will find the average and the sum of a series of
  234.            numbers in a DATA statement.
  235.         2. A number guessing game program.  (|Hint~: Have the computer pick a
  236.            number between 1 and 1000 and then INPUT guesses as to what the
  237.            number is.)
  238.  
  239. If you have trouble, type  |LOAD"AVERAGE"~ for the average program,
  240.                            |LOAD"GUESS_1"~ for one guessing game, or
  241.                            |LOAD"GUESS_2"~ for a different version of the game.
  242.  
  243. and then |LIST~ to give a listing of the sample program.  Type |NEW~ to erase the
  244. sample program and start one of your own.
  245.  
  246.     If you would like information on how to obtain a    |The PC-Prof.~
  247. copy of "Intermediate BASIC", volume two of the BASIC Prof.    |P.O. Box 26~
  248. series, send your name and address to:                |Salina, Ks.~
  249.                                 |67402-0026~
  250. -----
  251.